package cn.zhangjie.user.controller;

import cn.zhangjie.basic.exception.BusinssException;
import cn.zhangjie.basic.util.JsonResult;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.system.query.DetailQuery;
import cn.zhangjie.user.domain.User;
import cn.zhangjie.user.dto.RegisterDto;
import cn.zhangjie.user.query.UserQuery;
import cn.zhangjie.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class UserController {
    @Autowired
    private IUserService userService;

    //手机验证码注册接口
    @PostMapping("/phoneRegister")
    public JsonResult phoneRegister(@RequestBody RegisterDto dto){
        try {
            userService.phoneRegister(dto);
            return JsonResult.me();
        }catch (BusinssException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常，稍后重试！");
        }
    }



    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<User> findall(){
        return userService.findAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条" )
    public User findone(@PathVariable("id") Long id){
        return userService.findById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    public JsonResult delete(@PathVariable("id") Long id){

        try {
            userService.del(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult updataoradd(@RequestBody User user){

        try {
            if(user.getId()==null){
                userService.add(user);
                return JsonResult.me();
            }else {
                userService.update(user);
                return JsonResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<User> pagequery(@RequestBody UserQuery userQuery){
        try {
            return userService.queryPage(userQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult pactchdel(@RequestBody List<Long> ids){

        try {
            userService.pactchdel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("批量删除失败");
        }
    }







}
